﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace TPA.DataAccess
{
    public interface IProgramBLL
    {
        /// <summary>
        /// return programs whose name is specified programname
        /// </summary>
        /// <param name="programname"></param>
        /// <returns></returns>
        IQueryable<Program> GetPrograms(string programname);
        void Add(Program program);
        void SaveChanges();
        void Remove(Program program);
        Program Duplicate(Program program);
        Program Get(Guid Id);
        /// <summary>
        /// return programs whose name contain specified programname
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        IQueryable<Program> GetProgramsContain(string name);
        /// <summary>
        /// get all programs
        /// </summary>
        /// <returns>all programs</returns>
        IQueryable<Program> GetPrograms();
        void Refresh();
        void Dispose();
    }

    public class ProgramBLL:IProgramBLL
    {
        TVProgamAnalyticEntities myEntities = new TVProgamAnalyticEntities();

        public IQueryable<Program> GetPrograms(string programname)
        {
            //throw new NotImplementedException();
            var programs = from p in myEntities.Programs
                           where p.Name == programname
                           select p;
            return programs;
        }

        public void Add(Program program)
        {
            myEntities.AddToPrograms(program);
            //throw new NotImplementedException();
        }

        public void SaveChanges()
        {
            myEntities.SaveChanges();
            //throw new NotImplementedException();
        }

        public void Remove(Program program)
        {
            myEntities.DeleteObject(program);
            //throw new NotImplementedException();
        }

        public Program Duplicate(Program program)
        {
            Program p1 = new Program()
            {
                Name = program.Name,
                EnglishName = program.EnglishName,
                Description = program.Description,
                EnglishDescription = program.EnglishDescription,
                ProducingAreaId = program.ProducingAreaId,
                LengthInSeconds = program.LengthInSeconds,
                ProducingDate = program.ProducingDate,
                ProducingTypeId = program.ProducingTypeId,
                ProgramSourceId = program.ProgramSourceId,
                Tag = program.Tag,
                SubcategoryId = program.SubcategoryId
            };
            myEntities.AddToPrograms(p1);
            return p1;
                 
            //throw new NotImplementedException();
        }

        public Program Get(Guid id)
        {
            var program = from p in myEntities.Programs
                              where p.Id == id 
                              select p;
            if (program.Count() > 0)
                return program.Single();
            else
                return null;
            //throw new NotImplementedException();
        }


        public IQueryable<Program> GetProgramsContain(string name)
        {
            var programs = from p in myEntities.Programs
                           where p.Name.Contains(name)
                           select p;
            return programs;
            //throw new NotImplementedException();
        }


        public void Refresh()
        {
            myEntities.Refresh(RefreshMode.StoreWins, myEntities.Programs);
            //throw new NotImplementedException();
        }

        public void Dispose()
        {
            myEntities.Dispose();
        }


        public IQueryable<Program> GetPrograms()
        {
            return myEntities.Programs;//.ToList();
            //throw new NotImplementedException();
        }

    }
}
